package com.baimifan.renovation;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baimifan.renovation.adapter.TaskFragAdapter;
import com.baimifan.renovation.manager.HomeTwoClassifyManager;
import com.baimifan.renovation.tools.statusBar;
import com.flyco.tablayout.SlidingTabLayout;
import com.gyf.barlibrary.ImmersionBar;

import org.xutils.common.util.DensityUtil;

import java.lang.ref.WeakReference;

public class TaskActivity extends FragmentActivity implements View.OnClickListener{
    private Float w, h, widthPixels, heightPixels;
    private LinearLayout task_flag;
    private Activity task;
    private RelativeLayout task_rl;
    private ImageView task_tag;
    private TextView task_title;
    private ImageView task_search;
    private SlidingTabLayout task_tab;
    private ViewPager viewPager;
    private int cId;
    private String cName;

    private HomeTwoClassifyManager classifyManager;
    private TaskFragAdapter taskFragAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task);
        ImmersionBar.with(this).init();
        task = TaskActivity.this;
        init();
        getDisplay();
        setLayoutParams();
        cId = getIntent().getIntExtra("cId", 0);
        cName = getIntent().getStringExtra("catName");
        task_title.setText(cName);
        classifyManager.classify(cId+"");
    }

    private void init () {
        task_flag = (LinearLayout) findViewById(R.id.task_flag);
        task_rl = (RelativeLayout) findViewById(R.id.task_rl);
        task_tag = (ImageView) findViewById(R.id.task_tag);
        task_title = (TextView) findViewById(R.id.task_title);
        task_search = (ImageView) findViewById(R.id.task_search);
        task_tab = (SlidingTabLayout) findViewById(R.id.task_tab);
        viewPager = (ViewPager) findViewById(R.id.task_viewpager);
        classifyManager =new HomeTwoClassifyManager(new MyHandler(this));
        task_tag.setOnClickListener(this);
        task_search.setOnClickListener(this);
    }

    private void getDisplay() {
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        widthPixels = Float.valueOf(metrics.widthPixels);
        heightPixels = Float.valueOf(metrics.heightPixels);
        w = widthPixels / 360;
        h = heightPixels / 640;
    }

    private void setLayoutParams() {
        LinearLayout.LayoutParams params_flag = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                statusBar.getStatusBarHeight(task));
        task_flag.setLayoutParams(params_flag);

        LinearLayout.LayoutParams params_rl = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                (int)(h*44));
        task_rl.setLayoutParams(params_rl);

        RelativeLayout.LayoutParams params_tag = new RelativeLayout.LayoutParams((int)(w*8.5), (int)(h*16));
        params_tag.addRule(RelativeLayout.CENTER_VERTICAL);
        params_tag.setMargins((int)(w*15), 0, 0, 0);
        task_tag.setLayoutParams(params_tag);

        RelativeLayout.LayoutParams params_search= new RelativeLayout.LayoutParams((int)(w*19), (int)(h*19));
        params_search.addRule(RelativeLayout.CENTER_VERTICAL);
        params_search.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params_search.setMargins(0, 0, (int)(w*18.9), 0);
        task_search.setLayoutParams(params_search);

        LinearLayout.LayoutParams params_tab = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,(int)(h*44));
        task_tab.setLayoutParams(params_tab);
        task_tab.setTextSelectColor(Color.parseColor("#333333"));
        task_tab.setTextUnselectColor(Color.parseColor("#333333"));
        task_tab.setIndicatorColor(Color.parseColor("#FF5E32"));
        task_tab.setTabSpaceEqual(true);
        task_tab.setIndicatorHeight(4);
        task_tab.setIndicatorWidth(49);
        task_tab.setTextsize(17);
        task_tab.setIndicatorHeight(4);
        task_tab.setIndicatorWidth(49);
        task_tab.setTabSpaceEqual(true);

        LinearLayout.LayoutParams params_vp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params_vp.setMargins(0, (int)(12.6), 0, 0);
        viewPager.setLayoutParams(params_vp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.task_tag:
                finish();
                break;

            case R.id.task_search:
                Intent intent = new Intent(task, SearchActivity.class);
                task.startActivity(intent);
                break;
        }
    }

    private class MyHandler extends Handler {
        WeakReference<TaskActivity> mtash;

        public MyHandler (TaskActivity taskActivity) {
            mtash = new WeakReference<TaskActivity>(taskActivity);
        }
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            TaskActivity task = mtash.get();
            switch (msg.what) {
                case 9:
                    taskFragAdapter = new TaskFragAdapter(getSupportFragmentManager(), classifyManager.getclasses());
                    viewPager.setAdapter(taskFragAdapter);
                    task_tab.setViewPager(viewPager);
                    break;
            }
        }
    }
}
